﻿using System;
using TwinCAT.Ads;

namespace AdsPlcCommunication
{
    public class PlcCommunication
    {
        private TcAdsClient adsClient;
        private string amsNetId;
        private int port;

        public PlcCommunication(string amsNetId, int port)
        {
            this.amsNetId = amsNetId;
            this.port = port;
            adsClient = new TcAdsClient();
        }

        public bool Connect()
        {
            try
            {
                adsClient.Connect(amsNetId, port);
                return adsClient.IsConnected;
            }
            catch (Exception ex)
            {
                Console.WriteLine("连接到 PLC 时出错: " + ex.Message);
                return false;
            }
        }

        public void Disconnect()
        {
            if (adsClient.IsConnected)
            {
                adsClient.Dispose();
            }
        }

        // 读取布尔型变量
        public bool ReadBool(string variableName)
        {
            try
            {
                return (bool)adsClient.ReadAny(0,1, typeof(bool));
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取布尔型变量时出错: " + ex.Message);
                return false;
            }
        }

        // 写入布尔型变量
        public void WriteBool(string variableName, bool value)
        {
            try
            {
                adsClient.WriteAny(0, 1, value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入布尔型变量时出错: " + ex.Message);
            }
        }

        // 读取整型变量
        public int ReadInt(string variableName)
        {
            try
            {
                return (int)adsClient.ReadAny(0,1, typeof(int));
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取整型变量时出错: " + ex.Message);
                return 0;
            }
        }

        // 写入整型变量
        public void WriteInt(string variableName, int value)
        {
            try
            {
                adsClient.WriteAny(0,1, value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入整型变量时出错: " + ex.Message);
            }
        }

        // 读取浮点型变量
        public float ReadFloat(string variableName)
        {
            try
            {
                return (float)adsClient.ReadAny(0,1, typeof(float));
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取浮点型变量时出错: " + ex.Message);
                return 0.0f;
            }
        }

        // 写入浮点型变量
        public void WriteFloat(string variableName, float value)
        {
            try
            {
                adsClient.WriteAny(0,1, value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入浮点型变量时出错: " + ex.Message);
            }
        }
    }


    class Program
    {
        static void Main()
        {
            string amsNetId = "192.168.1.100.1.1"; // 替换为实际的 AmsNetId
            int port = 851; // 替换为实际的端口号

            PlcCommunication plcComm = new PlcCommunication(amsNetId, port);

            if (plcComm.Connect())
            {
                // 读取布尔型变量
                bool boolValue = plcComm.ReadBool("MAIN.MyBoolVariable");
                Console.WriteLine("布尔型变量的值: " + boolValue);

                // 写入布尔型变量
                plcComm.WriteBool("MAIN.MyBoolVariable", true);

                // 读取整型变量
                int intValue = plcComm.ReadInt("MAIN.MyIntVariable");
                Console.WriteLine("整型变量的值: " + intValue);

                // 写入整型变量
                plcComm.WriteInt("MAIN.MyIntVariable", 123);

                // 读取浮点型变量
                float floatValue = plcComm.ReadFloat("MAIN.MyFloatVariable");
                Console.WriteLine("浮点型变量的值: " + floatValue);

                // 写入浮点型变量
                plcComm.WriteFloat("MAIN.MyFloatVariable", 3.14f);

                plcComm.Disconnect();
            }

            Console.WriteLine("按任意键退出...");
            //Console.ReadKey();
        }
    }
}